package com.yqg.protection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yqg.protection.common.Result;
import com.yqg.protection.common.interceptor.SysLog;
import com.yqg.protection.entity.RecordNotice;
import com.yqg.protection.service.IRecordNoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * RecordNoticeController 前端控制器
 * @author 小宇
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/record-notice")
public class RecordNoticeController {

    @Resource
    private IRecordNoticeService recordNoticeService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @SysLog("新增或者修改通知记录")
    public Result save(@RequestBody RecordNotice recordNotice) {
        return Result.success(recordNoticeService.saveOrUpdate(recordNotice));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @SysLog("删除单个通知记录")
    public Result delete(@PathVariable Integer id) {
        return Result.success(recordNoticeService.removeById(id));
    }

    /**
     *  删除列表
     */
    @PostMapping("/deleteByIdList")
    @SysLog("删除多个通知记录")
    public Result deleteByIdList(@RequestBody List<Long> idList) {
        return Result.success(recordNoticeService.removeBatchByIds(idList));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<RecordNotice> queryWrapper = new QueryWrapper<>();
       List<RecordNotice> list=  recordNoticeService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(recordNoticeService.getById(id));
    }

    /**
    *  分页查询
    */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,String searchParam,String startTime, String endTime) {
        MPJLambdaWrapper<RecordNotice> queryWrapper  = new MPJLambdaWrapper<RecordNotice>()
                .and(StringUtils.isNotBlank(startTime), wrapper -> wrapper.ge(RecordNotice::getNoticeDate, startTime))
                .and(StringUtils.isNotBlank(endTime), wrapper -> wrapper.le(RecordNotice::getNoticeDate, endTime))
                .and(StringUtils.isNotBlank(searchParam),wrapper -> wrapper.like(RecordNotice::getTele, searchParam))
                .orderByDesc("id");
        return Result.success(recordNoticeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    @ApiOperation("导出数据")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       String searchParam,String startTime, String endTime) {
        recordNoticeService.export(response, searchParam, startTime, endTime);
    }

    @ApiOperation("导入数据")
    @PostMapping("/importData")
    @SysLog("导入通知记录")
    public void importData(MultipartFile file) {
        recordNoticeService.importData(file);
    }
}

